#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<fcntl.h>
 void sig_hup(int signo)
{
	printf("%d received,pid = %ld\n",signo,(long)getpid());
}

 void pr_ids(char *name)
{
	printf("%s: pid = %ld, ppid = %ld, pgrp = %ld, tpgrp = %ld\n",name,(long)getpid(),(long)getppid(),(long)getpgrp(),(long)tcgetpgrp(STDIN_FILENO));
	fflush(stdout);
}

int main(int argc,char **argv)
{
	char             c;
	pid_t            pid;

	pr_ids("parent");
	if((pid = fork()) < 0)
		printf("fork error\n");
	
	else if(pid > 0)
		sleep(5);
	else
	{
		pr_ids("child");

        signal(SIGHUP,sig_hup);

		kill(getpid(),SIGTSTP);
		pr_ids("child");
		if(read(STDIN_FILENO,&c,1) != 1)
			printf("read error %d on controlling TTY\n",errno);
	}
	return 0;
}
